package com.movecar.jpush;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

import com.movecar.ui.ActivityAddFriends;
import com.movecar.ui.GroupDetailActivity;
import com.movecar.ui.MainActivity;
import com.movecar.ui.MoreInfoManagelActivity;
import com.movecar.utils.DataStore;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则： 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "MyReceiver";

	@SuppressLint("NewApi")
	@Override
	public void onReceive(final Context context, Intent intent) {
		Bundle bundle = intent.getExtras();

		Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			Log.d(TAG, "接收Registration Id : " + regId);
			// send the Registration Id to your server...
		} else if (JPushInterface.ACTION_UNREGISTER.equals(intent.getAction())) {
			String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			Log.d(TAG, "接收UnRegistration Id : " + regId);
			// send the UnRegistration Id to your server...
		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			Log.d(TAG, "接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
			String type = "";
			String messageId = "";
			// Intent mIntent = new Intent("tongzhi");
			// mIntent.putExtras(bundle);
			// 获得SharedPreferences对象
			SharedPreferences preferences = context.getSharedPreferences("new_message", 0);
			// 获得可编辑对象
			SharedPreferences.Editor editor = preferences.edit();
			if (!"".equals(bundle.getString(JPushInterface.EXTRA_EXTRA))
					&& !"{}".equals(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
				try {
					JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
					type = json.getString("notifyFlag");
					if ("focusMessage".equals(type)) {
						messageId = json.getString("messageId");
						editor.putString("messageId", messageId);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

				editor.putString(type, type);
				editor.commit();
			}
			// 发送广播
			// context.sendBroadcast(mIntent);
			// processCustomMessage(context, bundle);

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			Log.d(TAG, "接收到推送下来的通知");
			int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);

			SharedPreferences preferences = context.getSharedPreferences("new_message_notify", 0);
			// 获得可编辑对象
			SharedPreferences.Editor editor = preferences.edit();
			editor.putString("type", bundle.getString(JPushInterface.EXTRA_EXTRA));
			editor.commit();

			Log.d(TAG, "接收到推送下来的通知的ID: " + notifactionId);

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			Log.d(TAG, "用户点击打开了通知");
			// 读取数据
			SharedPreferences preferences = context.getSharedPreferences("userId", 0);
			String userId = preferences.getString("userId", "");
			String mobile = preferences.getString("mobile", "");
			if (null == DataStore.getInstance().getUserId() || "".equals(DataStore.getInstance().getUserId())) {
				DataStore.getInstance().setUserId(userId);
				DataStore.getInstance().setMobile(mobile);
			}
			if (!com.movecar.utils.imageloader.Utils.isTopActivity(context)) {
				String messageId = null;
				// 打开自定义的Activity
				Intent i = new Intent(context, MainActivity.class);
				if (!"".equals(bundle.getString(JPushInterface.EXTRA_EXTRA))
						&& !"{}".equals(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
					try {
						JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
						Log.e("json------------------>", bundle.getString(JPushInterface.EXTRA_EXTRA));
						if ("focusMessage".equals(json.getString("notifyFlag"))) {
							messageId = json.getString("messageId");
							Log.e("messageId------------------>", messageId);
							i.putExtra("newMessageId", messageId);
						} else if ("newTrend".equals(json.getString("notifyFlag"))) {
							i.putExtra("newTrend", json.getString("notifyFlag"));
						} else if ("refereeDetail".equals(json.getString("notifyFlag"))) {
							i.putExtra("refereeDetail", json.getString("notifyFlag"));
							i.putExtra("csId", json.getString("csId"));
						} else if ("applyFriend".equals(json.getString("notifyFlag"))) {
							i.putExtra("applyFriend", json.getString("notifyFlag"));
							
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}

				}
				i.putExtra("not_in", true);
				i.putExtra("push", true);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				context.startActivity(i);
			} else {

				String messageId = null;
				// 打开自定义的Activity

				if (!"".equals(bundle.getString(JPushInterface.EXTRA_EXTRA))
						&& !"{}".equals(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
					try {
						JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
						Log.e("json------------------>", bundle.getString(JPushInterface.EXTRA_EXTRA));
						if ("focusMessage".equals(json.getString("notifyFlag"))) {
							Intent i = new Intent(context, GroupDetailActivity.class);
							messageId = json.getString("messageId");
							Log.e("messageId------------------>", messageId);
							i.putExtra("messageId", messageId);
							i.putExtra("not_in", false);
							i.putExtra("push", true);
							i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							context.startActivity(i);
						} else if ("newTrend".equals(json.getString("notifyFlag"))) {
							Intent i = new Intent(context, MainActivity.class);
							i.putExtra("newTrend", json.getString("notifyFlag"));
							i.putExtra("not_in", false);
							i.putExtra("push", true);
							i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
							context.startActivity(i);

						} else if ("refereeDetail".equals(json.getString("notifyFlag"))) {
							Intent i = new Intent(context, MoreInfoManagelActivity.class);
							i.putExtra("csId", json.getString("csId"));
							i.putExtra("push", true);
							i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							context.startActivity(i);
						} else if ("applyFriend".equals(json.getString("notifyFlag"))) {
							Intent i = new Intent(context, ActivityAddFriends.class);
							i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							context.startActivity(i);
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}

				}

			}

		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
			Log.d(TAG, "用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
			// 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity，
			// 打开一个网页等..

		} else {
			Log.d(TAG, "Unhandled intent - " + intent.getAction());
		}
	}

	// 打印所有的 intent extra 数据
	private static String printBundle(Bundle bundle) {
		StringBuilder sb = new StringBuilder();
		for (String key : bundle.keySet()) {
			if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
			} else {
				sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
			}
		}
		return sb.toString();
	}
}
